home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / IndexedAdaptor.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  2.7 KB  |  87 lines

  1. " ------------------------------------------------------------------
  2.   Class IndexedAdaptor provides the appearance of a ValueHolder, but 
  3.   redirects the value and value: methods to the target by sending 
  4.   at: and at:put:, respectively.
  5.  
  6.   When there is no target, the value is always nil; setValue: is a 
  7.   no-op; and value: only notifies the dependents.
  8.  
  9.   Instance Variables:
  10.     index <Integer> The index to adapt.
  11.  
  12.  
  13.   An IndexedAdaptor is used to get and set an element in a collection. 
  14.   It is typically created by sending a #subject: message to this 
  15.   class, with the collection as the argument.  It is then equipped with 
  16.   the index number or other lookup key of the desired element, via 
  17.   #forIndex:. 
  18.   An IndexedAdaptor can manage a collection element that is embedded 
  19.   multiple levels within the subject, via an access path.  It can also 
  20.   be told to withhold its update messages to avoid duplicating those 
  21.   sent by its subject.  See ProtocolAdaptor for a fuller discussion of 
  22.   these abilities. 
  23.   -------------------------------------------------------------------
  24. "
  25. Class IndexedAdaptor :ProtocolAdaptor ! index !
  26. [
  27.    forIndex: anIndex
  28.      " Create a new IndexedAdaptor and initialize the index to adapt. 
  29.      * The subject or subjectChannel and whether the subject sends
  30.      * updates must be initialized separately.
  31.      "
  32.      ^ self new setIndex: anIndex
  33. |
  34.    forIndex: anIndex accessPath: aSequencableCollection
  35.      " Create a new IndexedAdaptor and initialize the index to adapt.
  36.      * The subject or subjectChannel and whether the subject sends 
  37.      * updates must be initialized separately.
  38.      "
  39.      ^ (self accessPath: aSequencableCollection) forIndex: anIndex
  40. |
  41.    forIndex
  42.      " Answer the index we're adapting. "
  43.  
  44.      ^ index
  45. |
  46.    setIndex: anIndex
  47.  
  48.      index <- anIndex
  49. |
  50.    setValueUsingTarget: anObject to: newValue
  51.      " Set the value in anObject using at:put: "
  52.  
  53.      (anObject == nil)
  54.        ifFalse: [anObject at: index put: newValue]
  55. |
  56.    valueUsingTarget: anObject    
  57.      " Answer the value returned by sending anObject with at: "
  58.  
  59.      (anObject == nil)
  60.        ifFalse: [^anObject at: index]
  61.         ifTrue: [^nil]
  62. |
  63.    update: anAspect with: aParameter from: anObject
  64.  
  65.      (anObject == super subject)
  66.         ifTrue: [(anAspect == #at: and: [aParameter = index])
  67.                       ifTrue: [super dependents update: #value with: nil from: self]]
  68.  
  69.        ifFalse: [super update: anAspect with: aParameter from: anObject]
  70. |
  71.    printOn: aStream
  72.  
  73.      aStream print: self class.
  74.  
  75.      aStream nextPut: $(.
  76.  
  77.      self target printOn: aStream.
  78.  
  79.      aStream space.
  80.  
  81.      self printPathOn: aStream.
  82.  
  83.      aStream nextPutAll: index printString.
  84.  
  85.      aStream nextPut: $).
  86. ]
  87.